﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Net;
using System.Security.Cryptography;
using System.Text;

namespace Q_SMS
{
    class Tools
    {
        /// <summary>
        /// 随机数
        /// </summary>
        /// <returns></returns>
        public static long GetRandom()
        {
            return new Random().Next(999999) % 900000 + 100000;
        }
        /// <summary>
        /// 时间戳
        /// </summary>
        public static long GetCurTime()
        {
            Int32 unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
            return unixTimestamp;
        }

        /// <summary>
        /// 获取群发数组
        /// </summary>
        /// <param name="nationCode"></param>
        /// <param name="phoneNumbers"></param>
        /// <returns></returns>
        public static JArray PhoneNumbersToJSONArray(string nationCode, List<string> phoneNumbers)
        {
            JArray tel = new JArray();
            foreach (var item in phoneNumbers)
            {
                JObject telElement = new JObject();
                telElement.Add("nationcode", nationCode);
                telElement.Add("mobile", item);
                tel.Add(telElement);
            }
            return tel;
        }
        /// <summary>
        /// 获取参数数组
        /// </summary>
        /// <param name="parms"></param>
        /// <returns></returns>
        public static JArray ParmsToJSONArray(IList<string> parms)
        {
            JArray smsParms = new JArray();
            foreach (var item in parms)
            {
                smsParms.Add(item);
            }
            return smsParms;
        }
        /// <summary>
        /// 生成群发凭证
        /// </summary>
        /// <param name="appid"></param>
        /// <param name="random"></param>
        /// <param name="curTime"></param>
        /// <param name="phoneNumbers"></param>
        /// <returns></returns>
        public static string CalculateSig(string appkey, long random, long curTime, List<string> phoneNumbers)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var item in phoneNumbers)
            {
                sb.Append(item + ",");
            }
            //移除尾部逗号
            sb = sb.Remove(sb.Length - 1, 1);
            return StrToHash(String.Format(
                "appkey={0}&random={1}&time={2}&mobile={3}",
                appkey, random, curTime, sb.ToString()));
        }

        /// <summary>
        /// 二进制数值转十六进制
        /// </summary>
        /// <param name="byteArray"></param>
        /// <returns></returns>
        private static string ByteArrayToHex(byte[] byteArray)
        {
            string returnStr = "";
            if (byteArray != null)
            {
                for (int i = 0; i < byteArray.Length; i++)
                {
                    returnStr += byteArray[i].ToString("x2");
                }
            }
            return returnStr;
        }
        /// <summary>
        /// 字符串转哈希
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string StrToHash(string str)
        {
            SHA256 sha256 = SHA256Managed.Create();
            byte[] resultByteArray = sha256.ComputeHash(System.Text.Encoding.UTF8.GetBytes(str));
            return ByteArrayToHex(resultByteArray);
        }
        /// <summary>
        /// 获取请求头
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static HttpWebRequest GetPostHttp(string url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            return request;
        }
    }
}
